/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nsip.graph;

import java.awt.GridLayout;
import javax.swing.JFrame;
import se.sics.isl.gui.DotDiagram;

/**
 * 
 * @author nico.rehwaldt
 */
public class GraphWindow {

    private final JFrame frame;
    private DiagramRendererPair[] diagrams;
    
    public GraphWindow(String name, HistorybasedGraphRenderer ... renderers) {
        frame = new JFrame();
        frame.setLayout(new GridLayout(renderers.length / 6 + 1, 6));

        diagrams = new DiagramRendererPair[renderers.length];

        for (int i = 0; i < renderers.length; i++) {
            HistorybasedGraphRenderer renderer = renderers[i];
            DiagramRendererPair pair = new DiagramRendererPair(renderer);
            diagrams[i] = pair;
            frame.add(pair.diagram);
        }
        
        frame.setSize(Math.min(renderers.length, 6) * 180, (renderers.length / 6 + 1) * 150);
        frame.setTitle(name);
    }

    public void redraw() {
        for (DiagramRendererPair pair: diagrams) {
            pair.redraw();
        }
    }
    
    public void setVisible(boolean visible) {
        frame.setVisible(visible);
    }

    private class DiagramRendererPair {

        public final DotDiagram diagram;
        public final HistorybasedGraphRenderer renderer;

        private final GraphInfo[] graphs;
        
        public DiagramRendererPair(HistorybasedGraphRenderer renderer) {

            this.graphs = renderer.getGraphInfos();
            this.diagram = new DotDiagram(graphs.length);
            this.renderer = renderer;
            
            diagram.setToolTipVisible(true);
            diagram.setYLabel(renderer.getName());

            for (int i = 0; i < renderer.getConstants().length; i++) {
                ConstantInfo info = renderer.getConstants()[i];
                diagram.addConstant(info.getColor(), info.getValue());
            }

            for (int j = 0; j < graphs.length; j++) {
                diagram.setName(j, graphs[j].getLabel());
                diagram.setDotColor(j, graphs[j].getColor());
                diagram.setShowGrid(true);
            }
        }

        public void redraw() {
            for (int i = 0; i < graphs.length; i++) {
                diagram.setData(i, renderer.getGraph(i), 0, renderer.getDisplayLength());
            }
        }
    }
}
